package learning.groovy.collection

class MapDemo {
	static main(args){
		def myMap = [a:1, b:2, c:3]
		println myMap instanceof HashMap
		println myMap['a']
		
		def emptyMap = [:]
		println emptyMap.size()
		//0
		
		def explicitMap = new TreeMap()
		explicitMap.putAll(myMap)
		println explicitMap['a']
		//1
		
		def x = 'a'
		println (['x':1] == [x:1])
		println (['a':1] == [(x):1])//x是变量
		
		myMap = [a:1, b:2, c:3]
		println myMap.get('a')
		println myMap.get('d',0)
		println myMap.get('d')
		println myMap.d
		
		myMap['d'] = 1
		println myMap.d
		
		myMap = ['a.b':1]
		println myMap.'a.b'
		
		myMap = [a:1, b:2, c:3]
		println myMap.any{ entry -> entry.value > 2 }
		println myMap.every{ entry -> entry.key < 'd' }
		
//		def toSet(list){
//			new java.util.HashSet(list)
//		}
		
		myMap = [a:1, b:2, c:3]
		def store = ''
		myMap.each {entry -> 
			store += entry.key
			store += entry.value
		}
		println store
		
		store = ''
		myMap.each {key,value ->
			store += key
			store += value
		}
		println store
		
		store = ''
		for(key in myMap.keySet()){
			store += key
		}
		println store
		
		store = ''
		for(value in myMap.values()){
			store += value
		}
		println store
		
		myMap = [a:1, b:2, c:3]
		def doubled = myMap.collect{ entry -> entry.value *= 2 }
		println doubled instanceof List
		println doubled
		println doubled.every{item -> item %2 == 0}
		
	}
	
}
